package util

import (
	"reflect"
	"strings"
)

func GetGormFields(stc any) []string {
	typ:=reflect.TypeOf(stc)
	if typ.Kind()==reflect.Ptr {
		// 传入的是指针类型，先解析指针
		typ = typ.Elem()
	}
	if typ.Kind() == reflect.Struct {
		columns:=make([]string,0,typ.NumField())
		for i:=0;i<typ.NumField();i++{
			fieldType:=typ.Field(i)
			if fieldType.IsExported() {	//只关注可导出成员
				if fieldType.Tag.Get("gorm") == "-" {	// 不做映射
					continue
				}
				name := Camel2Snake(fieldType.Name)
				if len(fieldType.Tag.Get("gorm"))>0 {
					content:=fieldType.Tag.Get("gorm")
					if strings.HasPrefix(content, "column:") {
						content = content[7:]
						pos:=strings.Index(content,";")
						if pos>0 {
							name = content[0:pos]
						} else if pos<0 {
							name = content
						}
					}
				}
				columns = append(columns, name)
			}
		}
		return columns
	}
	return nil
}
